/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.logic.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import model.pojo.AboutUs;
import model.pojo.Admins;
import model.pojo.ApplicantCompanies;
import model.pojo.Applicants;
import model.pojo.CareerCategories;
import model.pojo.ContactUsMsg;
import model.pojo.Countries;
import model.pojo.EducationLevels;
import model.pojo.Grades;
import model.pojo.Industries;
import model.pojo.Institutes;
import model.pojo.News;
import model.pojo.NewsCategories;
import model.pojo.Privileges;
import model.pojo.Products;
import model.pojo.Services;
import model.pojo.Settings;
import model.pojo.StudyFields;
import model.pojo.Uploads;
import model.pojo.Users;
import model.pojo.Vacancies;
import model.utils.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Maha
 */
public class AdminFunctionality {

    /**
     * This is Hibernate Session
     */
    Session session;

    /**
     *
     * Contain Code to get SessionFactory, from ${@link HibernateUtil} class
     */
    public AdminFunctionality() {
        session = HibernateUtil.getSessionFactory().openSession();
    }

    public Admins login(String email, String password) {
        Admins user = null;
        try {
            Criteria criteria = session.createCriteria(Admins.class).add(Restrictions.ilike("adminEmail", email)).add(Restrictions.ilike("adminPassword", password));
            Iterator iterator = criteria.list().iterator();

            while (iterator.hasNext()) {
                user = (Admins) iterator.next();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return user;
    }

    public Vector<ContactUsMsg> getMembersMsgs() {
        Vector<ContactUsMsg> msgs = new Vector<ContactUsMsg>();
        Iterator iterator = session.createCriteria(ContactUsMsg.class).list().iterator();
        while (iterator.hasNext()) {
            msgs.add((ContactUsMsg) iterator.next());
        }
        return msgs;
    }

    public int addNews(News news) {
        int x;
        try {
            session.beginTransaction();
            session.save(news);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            x = 0;
        }
        return x;
    }

    public int updatePieceOfNews(News news) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update News set  newsTitle= :title , newsShortDescription=:ndesc,newsDescription=:av , "
                + "newsLogo=:nl ,showHome=:ref ,"
                + "creationDate=:cd ,newsImage=:show , newsCategories.newsCategoryId = :cid where newsId=:newsid";
        Query query = session.createQuery(hql);
        query.setString("title", news.getNewsTitle());
        query.setString("ndesc", news.getNewsShortDescription());
        query.setString("av", news.getNewsDescription());
        query.setString("nl", news.getNewsLogo());
        query.setString("ref", news.getShowHome());
        query.setString("cd", news.getCreationDate());
        query.setString("show", news.getNewsImage());
        query.setInteger("cid", news.getNewsCategories().getNewsCategoryId());
        query.setInteger("newsid", news.getNewsId());
        x = query.executeUpdate();
        t.commit();
        System.out.println(x);
        return x;
    }

    public int deleteNews(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  News a where a.newsId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public Vector<Privileges> getAdminPrivilidges(int AdminId) {
        Vector<Privileges> msgs = new Vector<Privileges>();

        String hqlQuery = "select  p from Privileges p join p.adminses a where a.adminId=:id ORDER BY p.parentId ASC";
        Query hql = session.createQuery(hqlQuery).setInteger("id", AdminId);
        Iterator iterator = hql.list().iterator();
        while (iterator.hasNext()) {
            Privileges object = (Privileges) iterator.next();
            msgs.add(object);
        }
        return msgs;
    }

    public int deleteMsg(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        String hqlQuery = "delete from ContactUsMsg where mgId=:id";
        Query hql = session.createQuery(hqlQuery).setParameter("id", id);
        x = hql.executeUpdate();
        transaction.commit();
        return x;
    }

    public Vector<Vacancies> getAllVacancies() {
        Vector<Vacancies> vac = new Vector<Vacancies>();
        Iterator iterator = session.createCriteria(Vacancies.class).list().iterator();
        while (iterator.hasNext()) {
            vac.add((Vacancies) iterator.next());
        }
        return vac;
    }

    public int addVacancy(Vacancies vac) {
        int x;
        try {
            session.beginTransaction();
            session.save(vac);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int deleteVacancy(int VacancyId) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Vacancies a where a.vacancyId=:id").setParameter("id", VacancyId);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int updateVacancy(Vacancies vacancy) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Vacancies set  vacancyTitle= :title , positionType=:pt,availableVacancyNumber=:av , deadline=:dl ,refrence=:ref ,description=:desc ,showVacancy=:show where vacancyId = :id";
        Query query = session.createQuery(hql);
        query.setString("title", vacancy.getVacancyTitle());
        query.setString("pt", vacancy.getPositionType());
        query.setString("dl", vacancy.getDeadline());
        query.setString("ref", vacancy.getRefrence());
        query.setString("desc", vacancy.getDescription());
        query.setString("show", vacancy.getShowVacancy());
        query.setInteger("av", vacancy.getAvailableVacancyNumber());
        query.setInteger("id", vacancy.getVacancyId());
        x = query.executeUpdate();
        t.commit();
        System.out.println(x);
        return x;
    }

    public int addCountry(Countries country) {
        int x;
        try {
            session.beginTransaction();
            session.save(country);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int updateCountry(Countries coun) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Countries set countryName = :title where countryId = :id";
        Query query = session.createQuery(hql);
        query.setString("title", coun.getCountryName());
        query.setInteger("id", coun.getCountryId());
        x = query.executeUpdate();
        t.commit();
        System.out.println(x);
        return x;
    }

    public int deleteCountry(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Countries a where a.countryId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int addGrade(Grades g) {
        int x;
        try {
            session.beginTransaction();
            session.save(g);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int updateGrade(Grades vacancy) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Grades set gradeName = :title where gradeId = :id";
        Query query = session.createQuery(hql);
        query.setString("title", vacancy.getGradeName());
        query.setInteger("id", vacancy.getGradeId());
        x = query.executeUpdate();
        t.commit();
        System.out.println(x);
        return x;
    }

    public int deleteGrade(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Grades a where a.gradeId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int addInstitute(Institutes g) {
        int x;
        try {
            session.beginTransaction();
            session.save(g);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int updateInstitute(Institutes vacancy) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Institutes set instituteName = :title where instituteId = :id";
        Query query = session.createQuery(hql);
        query.setString("title", vacancy.getInstituteName());
        query.setInteger("id", vacancy.getInstituteId());
        x = query.executeUpdate();
        t.commit();
        System.out.println(x);
        return x;
    }

    public int deleteInstitute(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Institutes a where a.instituteId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int addLevel(EducationLevels g) {
        int x;
        try {
            session.beginTransaction();
            session.save(g);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int updateLevel(EducationLevels vacancy) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update EducationLevels set educationLevelName = :title where educationLevelId = :id";
        Query query = session.createQuery(hql);
        query.setString("title", vacancy.getEducationLevelName());
        query.setInteger("id", vacancy.getEducationLevelId());
        x = query.executeUpdate();
        t.commit();
        System.out.println(x);
        return x;
    }

    public int deleteLevel(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  EducationLevels a where a.educationLevelId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int addStudyField(StudyFields g) {
        int x;
        try {
            session.beginTransaction();
            session.save(g);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int updateStudyField(StudyFields vacancy) {

        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update StudyFields set studyFieldName = :title where studyFieldId = :id";
        Query query = session.createQuery(hql);
        query.setString("title", vacancy.getStudyFieldName());
        query.setInteger("id", vacancy.getStudyFieldId());
        x = query.executeUpdate();
        t.commit();
        System.out.println(x);
        return x;

    }

    public int deleteStudyField(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  StudyFields a where a.studyFieldId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int addIndustry(Industries g) {
        int x;
        try {
            session.beginTransaction();
            session.save(g);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int updateIndustries(Industries vacancy) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Industries set industryTitle = :title where industryId = :id";
        Query query = session.createQuery(hql);
        query.setString("title", vacancy.getIndustryTitle());
        System.out.println(vacancy.getIndustryId());
        query.setInteger("id", vacancy.getIndustryId());
        x = query.executeUpdate();
        t.commit();
        System.out.println(x);
        return x;
    }

    public int deleteIndustry(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Industries a where a.industryId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int addCareerCategory(CareerCategories g) {
        int x;
        try {
            session.beginTransaction();
            session.save(g);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int updateCareerCategory(CareerCategories vacancy) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update CareerCategories set careerCategoryTitle = :title where careerCategoryId= :id";
        Query query = session.createQuery(hql);
        query.setString("title", vacancy.getCareerCategoryTitle());

        query.setInteger("id", vacancy.getCareerCategoryId());
        x = query.executeUpdate();
        t.commit();

        return x;
    }

    public int deleteCareerCategory(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  CareerCategories a where a.careerCategoryId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }
    //========================================================

    public int addNewsCategory(NewsCategories g) {
        int x;
        try {
            session.beginTransaction();
            session.save(g);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int updateNewsCategory(NewsCategories vacancy) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update NewsCategories set newsCategoryTitle = :title,comments=:comment,dueDate=:ddate,period=:per where newsCategoryId= :id";
        Query query = session.createQuery(hql);
        query.setString("title", vacancy.getNewsCategoryTitle());
        query.setString("comment", vacancy.getComments());
        query.setString("per", vacancy.getPeriod());
        query.setInteger("id", vacancy.getNewsCategoryId());
        query.setString("ddate", vacancy.getDueDate());
        x = query.executeUpdate();
        t.commit();

        return x;
    }

    public int deleteNewsCategory(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  NewsCategories a where a.newsCategoryId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public Vector<Admins> getAdmins() {
        Vector<Admins> admins = new Vector<Admins>();
        Iterator iterator = session.createCriteria(Admins.class).list().iterator();
        while (iterator.hasNext()) {
            admins.add((Admins) iterator.next());
        }

        return admins;
    }

    public Vector<Privileges> getPrivilidges() {
        Vector<Privileges> admins = new Vector<Privileges>();
        Iterator iterator = session.createCriteria(Privileges.class).list().iterator();
        while (iterator.hasNext()) {
            admins.add((Privileges) iterator.next());
        }

        return admins;
    }

    public int deleteAdmin(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Admins a where a.adminId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int removePriviled(int adminid, int privid) throws SQLException {
        int x = 0;
        session.getTransaction().begin();
        String sql = "delete from admin_privileges where admin_id=? and privilege_id=?";
        PreparedStatement prepareStatement = session.connection().prepareStatement(sql);
        prepareStatement.setInt(1, adminid);
        prepareStatement.setInt(2, privid);
        x = prepareStatement.executeUpdate();
        System.out.println("x=" + x);
        session.getTransaction().commit();
        return x;
    }

    public int addPriviled(int adminid, int privid) throws SQLException {
        int x = 0;
        session.getTransaction().begin();
        String selectQuery = "select * from admin_privileges where admin_id=? and privilege_id=?";
        PreparedStatement selectStatement = session.connection().prepareStatement(selectQuery);
        selectStatement.setInt(1, adminid);
        selectStatement.setInt(2, privid);
        ResultSet executeQuery = selectStatement.executeQuery();

        if (executeQuery.next() == true) {
            x = 0;
        } else {

            String sql = "insert into  admin_privileges values(?,?)";
            PreparedStatement prepareStatement = session.connection().prepareStatement(sql);
            prepareStatement.setInt(1, adminid);
            prepareStatement.setInt(2, privid);
            x = prepareStatement.executeUpdate();
        }
        session.getTransaction().commit();
        return x;
    }

    public int addAdmin(Admins g) {
        int x;
        try {
            session.beginTransaction();
            session.save(g);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int addService(Services vac) {
        int x;
        try {
            session.beginTransaction();
            session.save(vac);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public int deleteService(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Services a where a.serviceId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int deleteProduct(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Products a where a.productId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int addProduct(Products vac) {
        int x;
        try {
            session.beginTransaction();
            session.save(vac);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public Vector<Settings> getSetting() {
        Vector< Settings> s = new Vector<Settings>();
        Iterator iterator = session.createCriteria(Settings.class).list().iterator();
        while (iterator.hasNext()) {
            s.add((Settings) iterator.next());
        }
        return s;
    }

    public int updateSettings(Settings vacancy) throws Exception {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Settings set registerationEmails=:regmail,careerEmails=:cmail"
                + ",supplierEmails=:supmail,contactEmails=:conmail,uploadPath=:upath,maxPageRecords=:mpr,emailTime=:et,"
                + "smtpName=:sname,smtpUser=:suser,smtpPassword=:spw,projectName=:pname ,adminMail=:admail where emailId = :id";
        Query query = session.createQuery(hql);
        query.setString("regmail", vacancy.getRegisterationEmails());
        query.setString("cmail", vacancy.getCareerEmails());
        query.setString("supmail", vacancy.getSupplierEmails());
        query.setString("conmail", vacancy.getContactEmails());
        query.setString("upath", vacancy.getUploadPath());
        query.setInteger("mpr", vacancy.getMaxPageRecords());
        query.setString("et", vacancy.getEmailTime());
        query.setString("sname", vacancy.getSmtpName());
        query.setString("suser", vacancy.getSmtpUser());
        query.setString("spw", vacancy.getSmtpPassword());
        query.setString("pname", vacancy.getProjectName());
        query.setInteger("id", vacancy.getEmailId());
        query.setString("admail", vacancy.getAdminMail());
        x = query.executeUpdate();
        t.commit();

        return x;
    }

    public int updateAboutUs(AboutUs vacancy) throws Exception {
        int x = 0;

        Transaction t = session.beginTransaction();
        String hql = "update AboutUs set aboutUsTerm=:term,aboutUsDescription=:desc"
                + ",showMenu=:show  where aboutUsId = :id";
        Query query = session.createQuery(hql);
        query.setString("term", vacancy.getAboutUsTerm());
        query.setString("desc", vacancy.getAboutUsDescription());
        // query.setInteger("rank", vacancy.getAboutUsRank());
        query.setString("show", vacancy.getShowMenu());
        query.setInteger("id", vacancy.getAboutUsId());
        x = query.executeUpdate();
        System.out.println(x);
        t.commit();

        return x;
    }

    public Vector<Applicants> getSearchByYOE(int yoe) {
        Vector<Applicants> app = new Vector<Applicants>();
        Iterator iterator = session.createCriteria(Applicants.class).add(Restrictions.ge("yearsOfExperince", yoe)).list().iterator();
        while (iterator.hasNext()) {
            app.add((Applicants) iterator.next());
        }
        return app;
    }

    public Vector<ApplicantCompanies> getAppCompanies(int appId) {
        Vector<ApplicantCompanies> app = new Vector<ApplicantCompanies>();
        Iterator iterator = session.createCriteria(ApplicantCompanies.class).add(Restrictions.eq("applicantId", appId)).list().iterator();
        while (iterator.hasNext()) {
            app.add((ApplicantCompanies) iterator.next());
        }
        return app;
    }

    public Vector<Applicants> getSearchByEL(int yoe) {
        Vector<Applicants> app = new Vector<Applicants>();
        Iterator iterator = session.createCriteria(Applicants.class).add(Restrictions.eq("educationLevels.educationLevelId", yoe)).list().iterator();
        while (iterator.hasNext()) {
            app.add((Applicants) iterator.next());
        }
        return app;
    }

    public Vector<Applicants> getSearchVacancies(int yoe) {
        Vector<Applicants> app = new Vector<Applicants>();
        Iterator iterator = session.createCriteria(Applicants.class).add(Restrictions.eq("vacancies.vacancyId", yoe)).list().iterator();
        while (iterator.hasNext()) {
            app.add((Applicants) iterator.next());
        }
        return app;
    }

    public Vector<Applicants> getSearchBySL(int yoe) {
        Vector<Applicants> app = new Vector<Applicants>();
        Iterator iterator = session.createCriteria(Applicants.class).add(Restrictions.eq("studyFields.studyFieldId", yoe)).list().iterator();
        while (iterator.hasNext()) {
            app.add((Applicants) iterator.next());
        }
        return app;
    }

    public Vector<Applicants> getSearchByGY(String yoe) {
        Vector<Applicants> app = new Vector<Applicants>();
        Iterator iterator = session.createCriteria(Applicants.class).add(Restrictions.ge("graduationYear", yoe)).list().iterator();
        while (iterator.hasNext()) {
            app.add((Applicants) iterator.next());
        }
        return app;
    }

    public int deleteApplicant(int id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Applicants a where a.applicantId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public int deleteUser(long id) {
        int x = 0;
        Transaction transaction = session.beginTransaction();
        Query del = session.createQuery("delete  Users a where a.userId=:id").setParameter("id", id);
        x = del.executeUpdate();
        transaction.commit();
        return x;
    }

    public Vector<Applicants> getApplicants() {
        Vector<Applicants> applicant = new Vector<Applicants>();
        Iterator iterator = session.createCriteria(Applicants.class).list().iterator();
        while (iterator.hasNext()) {
            applicant.add((Applicants) iterator.next());
        }
        return applicant;
    }

    public Vector<Users> getUsers() {
        Vector<Users> applicant = new Vector<Users>();
        Iterator iterator = session.createCriteria(Users.class).list().iterator();
        while (iterator.hasNext()) {
            applicant.add((Users) iterator.next());
        }
        return applicant;
    }

    public int insertUpload(Uploads upload) {
        int x;
        try {
            session.beginTransaction();
            session.save(upload);
            session.getTransaction().commit();
            x = 1;
        } catch (Exception e) {
            e.printStackTrace();
            x = 0;
        }
        return x;
    }

    public Vector<Uploads> getUploads() {
        Vector<Uploads> uploads = new Vector<Uploads>();
        Iterator iterator = session.createCriteria(Uploads.class).list().iterator();
        while (iterator.hasNext()) {
            uploads.add((Uploads) iterator.next());

        }
        return uploads;
    }

    public int updateUser(Users vacancy) throws Exception {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Users set userName=:un"
                + ",userEmail=:mail,userPassword=:pw,userType=:type,question=:q,answer=:answer"
                + " where userId=:userId";
        Query query = session.createQuery(hql);
        query.setString("un", vacancy.getUserName());
        query.setString("mail", vacancy.getUserEmail());
        query.setString("pw", vacancy.getUserPassword());
        query.setString("type", vacancy.getUserType());
        query.setString("q", vacancy.getQuestion());
        query.setString("answer", vacancy.getAnswer());
        query.setLong("userId", vacancy.getUserId());

        x = query.executeUpdate();
        t.commit();

        return x;
    }
    //=============================================================

    public int updateService(Services vacancy) throws Exception {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Services set serviceName=:sname"
                + ",serviceLogo=:slogo,serviceDescription=:sd,serviceImage=:si,serviceShortDescription=:ssd,showMenu=:sm"
                + " where serviceId=:serviceId";
        Query query = session.createQuery(hql);
        query.setString("sname", vacancy.getServiceName());
        query.setString("slogo", vacancy.getServiceLogo());
        query.setString("sd", vacancy.getServiceDescription());
        query.setString("si", vacancy.getServiceImage());
        query.setString("ssd", vacancy.getServiceShortDescription());
        query.setString("sm", vacancy.getShowMenu());
        query.setLong("serviceId", vacancy.getServiceId());

        x = query.executeUpdate();
        t.commit();

        return x;
    }

    public Vector<Services> getService(int id) throws Exception {
        Vector<Services> service = new Vector<Services>();
        Iterator iterator = session.createCriteria(Services.class).add(Restrictions.eq("serviceId", id)).list().iterator();
        while (iterator.hasNext()) {
            service.add((Services) iterator.next());

        }
        return service;
    }

    public Vector<Products> getProduct(int id) throws Exception {
        Vector<Products> service = new Vector<Products>();
        Iterator iterator = session.createCriteria(Products.class).add(Restrictions.eq("productId", id)).list().iterator();
        while (iterator.hasNext()) {
            service.add((Products) iterator.next());

        }
        return service;
    }

    public int updateProduct(Products vacancy) throws Exception {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update Products set productName=:sname"
                + ",productLogo=:slogo,productDescription=:sd,productImage=:si,productShortDescription=:ssd,showMenu=:sm"
                + " where productId=:productId";
        Query query = session.createQuery(hql);
        query.setString("sname", vacancy.getProductName());
        query.setString("slogo", vacancy.getProductLogo());
        query.setString("sd", vacancy.getProductDescription());
        query.setString("si", vacancy.getProductImage());
        query.setString("ssd", vacancy.getProductShortDescription());
        query.setString("sm", vacancy.getShowMenu());
        query.setLong("productId", vacancy.getProductId());

        x = query.executeUpdate();
        t.commit();

        return x;
    }
    private Integer newsId;
    private NewsCategories newsCategories;
    private String newsTitle;
    private String newsShortDescription;
    private String newsDescription;
    private String newsLogo;
    private String showHome;
    private String creationDate;
    private String newsImage;

    public int updateNews(News news) {
        int x = 0;
        Transaction t = session.beginTransaction();
        String hql = "update News set newsCategories.newsCategoryId=:catId"
                + ",newsTitle=:title,newsShortDescription=:nd,newsDescription=:np,newsLogo=:nl,showHome=:sh,creationDate=:cd,newsImage=:img"
                + " where newsId=:nid";
        Query query = session.createQuery(hql);
        query.setInteger("catId", news.getNewsCategories().getNewsCategoryId());
        query.setString("title", news.getNewsTitle());
        query.setString("nd", news.getNewsShortDescription());
        query.setString("np", news.getNewsDescription());
        query.setString("nl", news.getNewsLogo());
        query.setString("sh", news.getShowHome());
        query.setString("cd", news.getCreationDate());
        query.setString("img", news.getNewsImage());
        query.setLong("nid", news.getNewsId());
        x = query.executeUpdate();
        t.commit();

        return x;
    }
}
